/*
 * CS_REQ_DAILY_MISSION.cpp
 *
 *  Created on: 2014. 4. 9.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/DailyMission.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendFailDailyMission(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_REQ_DAILY_MISSION);
	pUser->Send(pAck);
}

void Command::CS_REQ_DAILY_MISSION(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_REQ_DAILY_MISSION			0x0960
	{

	}

	SC_REQ_DAILY_MISSION			0x0961
	{
		s16 s16ack;
		u8 u8special_reward_state;		/// 0: 미완료 1: 받기가능 2: 보상완료
		u32 u32remain_time;				/// 남은 시간 초단위
		u8 u8count;
		{
			u16 u16mission_idx;		/// 일일미션 테이블 아이디
			u8	u8achive_current;	/// 현재 달성도
			u8	u8achive_max;		/// 최대 달성도

			u8 u8reward_type; 		/// 0: 아이템, 1: 골드, 2: 경험치, 3: 스태미너???
			u32 u32reward_value;	/// 보상 갯수
			s8 s8class_type;		/// 클래스 타입
			u8 u8sheet_type;		/// 골드일땐 잡화에 있는 골드 TID
			u16	u16tid;				/// 테이블 아이디

			u8 u8mission_state;		/// 0: 미완료 1: 받기가능 2: 보상완료
		}
	}*/

	PREPARE_PACKET;

	if (false == NResource::Static::Instance().GetContentsOpenResource()->IsOpen(static_cast<int>(ContentsOpen::DAILY_MISSION)))
	{
		DEBUGLOG("%s : Contents is Not Open", __FUNCTION__);
		SendFailDailyMission(pUser, ACK_NOT_OPEN);
		return;
	}

	NLogic::DailyMission * pDailyMission = pUser->GetDailyMission();
	if (NULL == pDailyMission)
	{
		DEBUGLOG("%s : pDailMission is NULL", __FUNCTION__);
		SendFailDailyMission(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	pDailyMission->CheckMissionInitialize();

	// 스페셜 미션 데이터
	NLogic::DailyMission::Data * pSpecialData = pDailyMission->GetData(NLogic::DailyMission::MissionType::SPECIAL_MISSION);
	if (NULL == pSpecialData)
	{
		DEBUGLOG("%s : pSpecialData is NULL", __FUNCTION__);
		SendFailDailyMission(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<uint8_t>(pSpecialData->isReward);

	int remainSec = NLogic::NFunction::GetNextDailyMissionInitTime() - time(NULL);
	*pAck << static_cast<uint32_t>(remainSec);

	int missionCount = static_cast<int>(NLogic::DailyMission::MissionType::__MAX__) - 1;
	*pAck << static_cast<uint8_t>(missionCount);

	int startIndex	= static_cast<int>(NLogic::DailyMission::MissionType::MISSION1);
	int endIndex	= static_cast<int>(NLogic::DailyMission::MissionType::__MAX__);

	for (; startIndex < endIndex; startIndex++)
	{
		NLogic::DailyMission::Data * pData = pDailyMission->GetData(static_cast<NLogic::DailyMission::MissionType>(startIndex));
		if (NULL == pData)
		{
			DEBUGLOG("%s : pData is NULL! MissionIndex=%d", __FUNCTION__, startIndex);
			::Network::GETPACKETPOOL()->SEND_DEL(pAck);
			SendFailDailyMission(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		NResource::DailyMissionResource::Flyweight * pFly = NResource::Static::Instance().GetDailyMissionResource()->GetFly(startIndex);
		if (NULL == pFly)
		{
			DEBUGLOG("%s : pFly is NULL! MissionIndex=%d", __FUNCTION__, startIndex);
			::Network::GETPACKETPOOL()->SEND_DEL(pAck);
			SendFailDailyMission(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		*pAck << static_cast<uint16_t>(startIndex);			// 일일 미션 ID
		*pAck << static_cast<uint8_t>(pData->completeCount);	// 현재 달성도
		*pAck << static_cast<uint8_t>(pFly->_COMPLETE_COUNT);	// 최대 달성도
		*pAck << static_cast<uint8_t>(pFly->_KIND);			// 보상 종류 (0: 아이템, 1: 골드, 2: 경험치, 3: 스태미너)

		if (2 == pFly->_KIND)
		{
			int rewardExp = static_cast<int>((pUser->GetCharacterInfo().level * pFly->_VALUE * sqrt(pUser->GetCharacterInfo().level) ) / 150) * 100;
			*pAck << static_cast<uint32_t>(rewardExp);			// 보상 수량
		}
		else
		{
			*pAck << static_cast<uint32_t>(pFly->_VALUE);			// 보상 수량
		}
		*pAck << static_cast<int8_t>(pFly->_ITEM_CLASS);
		*pAck << static_cast<uint8_t>(pFly->_ITEM_SHEET);
		*pAck << static_cast<uint16_t>(pFly->_ITEM_TID);
		*pAck << static_cast<uint8_t>(pData->isReward);
	}

	pAck->MakePacket(CMD_SC_REQ_DAILY_MISSION);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
